package com.gdth.service;

import com.gdth.log.LogCollectorThread;
import com.gdth.log.LogServer;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

public class LogService extends Service {
    private static final String TAG = "LogService";
    
	/** SDcard状态监测 **/
    private SDStateMonitorReceiver sdStateReceiver;
    private LogTaskReceiver logTaskReceiver;
    
    private WakeLock wakeLock;
    
    private LogServer mLogServer;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        
        init();
    }
    
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mLogServer.recordLogServiceLog("LogService onDestroy");
        mLogServer.onDestroy();

        unregisterReceiver(sdStateReceiver);
        unregisterReceiver(logTaskReceiver);
    }

    /**
     * 初始化
     */
	private void init(){
		PowerManager pm = (PowerManager) getApplicationContext()
               .getSystemService(Context.POWER_SERVICE);
		wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
       
		mLogServer = new LogServer(wakeLock, this);

        register();
	}

   /**
    * 初始化广播
    */
    private void register() {
        IntentFilter sdCarMonitorFilter = new IntentFilter();
        sdCarMonitorFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        sdCarMonitorFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        sdCarMonitorFilter.addDataScheme("file");
        sdStateReceiver = new SDStateMonitorReceiver();
        registerReceiver(sdStateReceiver, sdCarMonitorFilter);

        IntentFilter logTaskFilter = new IntentFilter();
        logTaskFilter.addAction(LogServer.MONITOR_LOG_SIZE_ACTION);
        logTaskFilter.addAction(LogServer.SWITCH_LOG_FILE_ACTION);
        logTaskReceiver = new LogTaskReceiver();
        registerReceiver(logTaskReceiver, logTaskFilter);
    }


    /**
     * 监控SD卡状态
     * @author Administrator
     */
    class SDStateMonitorReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {

            if (Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())) { // 存储卡被卸载
                if (mLogServer.CURR_LOG_TYPE == mLogServer.SDCARD_TYPE) {
                    Log.d(TAG, "SDcar is UNMOUNTED");
                    mLogServer.CURR_LOG_TYPE = mLogServer.MEMORY_TYPE;
                    new LogCollectorThread(wakeLock, mLogServer).start();
                }
            } else { // 存储卡被挂载
                if (mLogServer.CURR_LOG_TYPE == mLogServer.MEMORY_TYPE) {
                    Log.d(TAG, "SDcar is MOUNTED");
                    mLogServer.CURR_LOG_TYPE = mLogServer.SDCARD_TYPE;
                    new LogCollectorThread(wakeLock, mLogServer).start();
                }
            }
        }
    }

    /**
     * 日志任务接收 切换日志，监控日志大小
     * @author Administrator
     */
    class LogTaskReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (LogServer.SWITCH_LOG_FILE_ACTION.equals(action)) {
                new LogCollectorThread(wakeLock, mLogServer).start();
            } else if (LogServer.MONITOR_LOG_SIZE_ACTION.equals(action)) {
                mLogServer.checkLogSize();
            }
        }
    }
}